/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.hazards.flood.hazard;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxDateValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.hazards.flood.FloodConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.FloodHazard;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class FloodHazardForm extends AbstractForm implements BuildingUpdate {
	
	private Label FIRMPanel;
	private Label FIRMCommunity;
	private Label FIRMEffectiveDate;
	private Label FIRMRevisedDate;
	private Label floodHazardZone;
	private Label baseFloodElevation;
	private Label verticalDatum;
	private Label otherVerticalDatum;
	private Label baseFloodElevationPlus;
	private Label lowestGradeElevation;
	
	// form fields
	private TextBox formFIRMPanel = new TextBox();
	private TextBox formFIRMCommunity = new TextBox();
	private TextBox formFIRMEffectiveDate = new TextBox();
	private TextBox formFIRMRevisedDate = new TextBox();
	private FloodHazardZoneListBox formFloodHazardZone = new FloodHazardZoneListBox();
	private TextBox formBaseFloodElevation = new TextBox();
	private FloodVerticalDatumListBox formVerticalDatum = new FloodVerticalDatumListBox();
	private TextBox formOtherVerticalDatum = new TextBox();
	private TextBox formBaseFloodElevationPlus = new TextBox();
	private TextBox formLowestGradeElevation = new TextBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(10,2);
	
	// entities
	private FloodHazard floodHazard;
	
	// listeners
	private FloodHazardListBoxChangeListener floodHazardListBoxChangeListener;
	private FloodHazardTextBoxKeyListener floodHazardTextBoxKeyListener;
	private TextBoxDateValueBlurHandler textBoxDateValueBlurHandler;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	private FloodConstants constants;
	
	public FloodHazardForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (FloodConstants)GWT.create(FloodConstants.class);
		tabTitle = constants.floodHazardTabTitle();
		
		FIRMPanel = new Label(constants.floodHazardFIRMPanel());
		FIRMCommunity = new Label(constants.floodHazardFIRMCommunity());
		FIRMEffectiveDate = new Label(constants.floodHazardFIRMEffectiveDate());
		FIRMRevisedDate = new Label(constants.floodHazardFIRMRevisedDate());
		floodHazardZone = new Label(constants.floodHazardFloodHazardZone());
		baseFloodElevation = new Label(constants.floodHazardBaseFloodElevation());
		verticalDatum = new Label(constants.floodHazardVerticalDatum());
		otherVerticalDatum = new Label(constants.floodHazardOtherVerticalDatum());
		baseFloodElevationPlus = new Label(constants.floodHazardBaseFloodElevationPlus());
		lowestGradeElevation = new Label(constants.floodHazardLowestGradeElevation());

		// add labels to form
		table.setWidget(0, 0, FIRMPanel);
		table.setWidget(1, 0, FIRMCommunity);
		table.setWidget(2, 0, FIRMEffectiveDate);
		table.setWidget(3, 0, FIRMRevisedDate);
		table.setWidget(4, 0, floodHazardZone);
		table.setWidget(5, 0, baseFloodElevation);
		table.setWidget(6, 0, verticalDatum);
		table.setWidget(7, 0, otherVerticalDatum);
		table.setWidget(8, 0, baseFloodElevationPlus);
		table.setWidget(9, 0, lowestGradeElevation);
		
		// set up form fields
		floodHazard = building.getFloodHazard();
		floodHazardListBoxChangeListener = new FloodHazardListBoxChangeListener(floodHazard);
		floodHazardTextBoxKeyListener = new FloodHazardTextBoxKeyListener(floodHazard);
		textBoxDateValueBlurHandler = new TextBoxDateValueBlurHandler();
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// listeners
		loadListeners();
		
		// forms
		loadForms();
		
		// add fields to table
		table.setWidget(0, 1, formFIRMPanel);
		table.setWidget(1, 1, formFIRMCommunity);
		table.setWidget(2, 1, formFIRMEffectiveDate);
		table.setWidget(3, 1, formFIRMRevisedDate);
		table.setWidget(4, 1, formFloodHazardZone);
		table.setWidget(5, 1, formBaseFloodElevation);
		table.setWidget(6, 1, formVerticalDatum);
		table.setWidget(7, 1, formOtherVerticalDatum);
		table.setWidget(8, 1, formBaseFloodElevationPlus);
		table.setWidget(9, 1, formLowestGradeElevation);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);	
		
	}
	
	private String getFormattedDate(Date date)
	{
		String dateString = "";
		if( date != null )
		{
			dateString = 	(date.getMonth() + 1)  + 
							"/" + date.getDate() + 
							"/" + (date.getYear() + 1900);
		}
		return dateString;
	}

	public void updateBuilding(Building building) {

		// entities
		setBuilding(building);
		this.floodHazard = building.getFloodHazard();
		
		// listeners 
		floodHazardListBoxChangeListener.updateFloodHazard(floodHazard);
		floodHazardTextBoxKeyListener.updateFloodHazard(floodHazard);
		
		// reload forms
		loadForms();
	}
	
	public void loadForms()
	{
		if( floodHazard != null)
		{
			formFIRMPanel.setText( floodHazard.getFIRMPanel() );
			formFIRMPanel.setName(FloodHazard.FIRMPANEL);
			
			formFIRMCommunity.setText( floodHazard.getFIRMCommunity() );
			formFIRMCommunity.setName(FloodHazard.FIRMCOMMUNITY);
			
			// format dates, if date is null, show nothing in field
			String effective = "";
			if( floodHazard.getFIRMEffectiveDate() != null )
			{
				effective = this.getFormattedDate( floodHazard.getFIRMEffectiveDate() ); 
			}
			String revised = "";
			if( floodHazard.getFIRMRevisedDate() != null )
			{
				revised = this.getFormattedDate( floodHazard.getFIRMRevisedDate() );
			}
			
			formFIRMEffectiveDate.setText( effective );
			formFIRMEffectiveDate.setName(FloodHazard.FIRMEFFECTIVE);
			
			formFIRMRevisedDate.setText( revised );
			formFIRMRevisedDate.setName(FloodHazard.FIRMREVISED);
			
			formFloodHazardZone.setListItem( floodHazard.getFloodHazardZone() );
			formFloodHazardZone.setName(FloodHazard.FLOODHAZARDZONE);
			
			String baseFloodElevation = "";
			if( floodHazard.getBaseFloodElevation() != null )
			{
				baseFloodElevation = floodHazard.getBaseFloodElevation().toString();
			}
			formBaseFloodElevation.setText( baseFloodElevation );
			formBaseFloodElevation.setName(FloodHazard.BASEFLOODELEVATION);
			
			formVerticalDatum.setListItem( floodHazard.getVerticalDatum() );
			formVerticalDatum.setName(FloodHazard.VERTICALDATUM);
			
			formOtherVerticalDatum.setText( floodHazard.getOtherVerticalDatum() );
			formOtherVerticalDatum.setName(FloodHazard.OTHERVERTICALDATUM);
			
			String baseFloodElevationPlus = "";
			if( floodHazard.getBaseFloodElevationPlus() != null )
			{
				baseFloodElevationPlus = floodHazard.getBaseFloodElevationPlus().toString();
			}
			formBaseFloodElevationPlus.setText( baseFloodElevationPlus );
			formBaseFloodElevationPlus.setName(FloodHazard.BASEFLOODELEVATIONPLUS);
			
			String lowestGradeElevation = "";
			if( floodHazard.getLowestGradeElevation() != null )
			{
				lowestGradeElevation = floodHazard.getLowestGradeElevation().toString();
			}
			formLowestGradeElevation.setText( lowestGradeElevation );
			formLowestGradeElevation.setName(FloodHazard.LOWESTGRADEELEVATION);
		}
		
		if( building != null)
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formFIRMPanel.setEnabled(false);
	            formFIRMCommunity.setEnabled(false);
	            formFIRMEffectiveDate.setEnabled(false);
	            formFIRMRevisedDate.setEnabled(false);
	            formFloodHazardZone.setEnabled(false);
	            formBaseFloodElevation.setEnabled(false);
	            formVerticalDatum.setEnabled(false);
	            formOtherVerticalDatum.setEnabled(false);
	            formBaseFloodElevationPlus.setEnabled(false);
	            formLowestGradeElevation.setEnabled(false);
	        }
	        else
	        {
	            formFIRMPanel.setEnabled(true);
	            formFIRMCommunity.setEnabled(true);
	            formFIRMEffectiveDate.setEnabled(true);
	            formFIRMRevisedDate.setEnabled(true);
	            formFloodHazardZone.setEnabled(true);
	            formBaseFloodElevation.setEnabled(true);
	            formVerticalDatum.setEnabled(true);
	            formOtherVerticalDatum.setEnabled(true);
	            formBaseFloodElevationPlus.setEnabled(true);
	            formLowestGradeElevation.setEnabled(true);            
	        }
		}
	}
	
	public void loadListeners()
	{
		formFIRMPanel.addFocusListener(floodHazardTextBoxKeyListener);
		formFIRMCommunity.addFocusListener(floodHazardTextBoxKeyListener);

		formFIRMEffectiveDate.addFocusListener(floodHazardTextBoxKeyListener);
		formFIRMEffectiveDate.addBlurHandler(textBoxDateValueBlurHandler);
		
		formFIRMRevisedDate.addFocusListener(floodHazardTextBoxKeyListener);
		formFIRMRevisedDate.addBlurHandler(textBoxDateValueBlurHandler);
		
		formFloodHazardZone.addChangeListener(floodHazardListBoxChangeListener);
		
		formBaseFloodElevation.addFocusListener(floodHazardTextBoxKeyListener);
		formBaseFloodElevation.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formVerticalDatum.addChangeListener(floodHazardListBoxChangeListener);
		
		formOtherVerticalDatum.addFocusListener(floodHazardTextBoxKeyListener);
		
		formBaseFloodElevationPlus.addFocusListener(floodHazardTextBoxKeyListener);
		formBaseFloodElevationPlus.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formLowestGradeElevation.addFocusListener(floodHazardTextBoxKeyListener);
		formLowestGradeElevation.addBlurHandler(textBoxIntegerValueBlurHandler);
		
	}
}
